﻿using System;
using System.Collections.Generic;
using System.IO;
using BasicFramework.Devices;
using SlimDX;
using BasicFramework.Services.MeshLoader.Nodes;

namespace BasicFramework.Services.MeshLoader.Meshes
{
    public class ColladaFile<T> : IDisposable where T : DeviceContextBase
    {
        #region Fields

        private ColladaModel model;
        private List<Mesh> meshes;
        private Boolean disposed;



        #endregion

        #region Properties

        #endregion

        #region Constructors

        public ColladaFile(string fileName, Vector3 position, DeviceContextBase context)
        {
            this.meshes = new List<Mesh>();

            this.model = new ColladaModel(fileName);


            foreach (MeshContainer m in model.meshes)
            {
                m.Position = position;
                m.BasePath = Path.GetDirectoryName(fileName);
                foreach (SceneNode s in model.SceneNodes)
                {
                    if (m.Name == s.GeometryLink)
                    {
                        m.ObjectMatrix = s.World;
                    }
                }

                Mesh newMesh = null;


                if (m.Textures.Count > 0)
                {
                    newMesh = new TexturedMesh(m, context);
                }
                else
                {
                    newMesh = new ColoredMesh(m, context);
                }

                if (newMesh != null)
                {
                    meshes.Add(newMesh);
                }
            }

        }


        #endregion

        #region Disposing

        ~ColladaFile()
        {
            Dispose(false);
        }

        protected virtual void Dispose(Boolean disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed 
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.

                }

                // Release unmanaged resources. If disposing is false, 
                // only the following code is executed.

            }
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion


        #region Events


        #endregion

        #region Methods




        public void SetPosition(Vector3 pos)
        {
            foreach (Mesh m in this.meshes)
            {
                m.Position = pos;
            }
        }

        public void SetProjection(Matrix projection)
        {
            foreach (Mesh m in this.meshes)
            {
                m.Projection = projection;
            }
        }

        public void SetView(Matrix view)
        {
            foreach (Mesh m in this.meshes)
            {
                m.View = view;
            }
        }

        public void UpdateAnimation(float frameTime)
        {

            this.model.UpdateAnimation(Matrix.Identity);
            //Render all submeshes);
        }

        public void Render()
        {
            //Render all submeshes
            foreach (Mesh m in this.meshes)
            {
                m.Render();
            }
        }


        #endregion
    }
}

